package com.yksj.healthtalk.ui.doctorstation;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.CommonExplainActivity;
import com.yksj.healthtalk.entity.ServiceTypeEntity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * VIP服务
 * 
 * @author Administrator
 * 
 */
public class DoctorMarkCommon extends BaseFragmentActivity implements
		OnClickListener {

	private TextView mEditText;
	private String mServiceTypeId;
	private String mData;
	private LinearLayout mView;//添加的父VIEW
	private List<ServiceTypeEntity> entities;
	
	@Override
	protected void onCreate(Bundle save) {
		super.onCreate(save);
		if(save!=null){
			mServiceTypeId=save.getString("SERVICE_TYPE_ID");
			mData=save.getString("CONTENT");
		}
		setContentView(R.layout.doctor_mark_conmmon_talk);
		
		initTitle();
		
		titleLeftBtn.setOnClickListener(this);	
		titleTextV.setText(getIntent().getStringExtra("title"));
		
		mEditText = (TextView)findViewById(R.id.docotr_content);
		mView = (LinearLayout) findViewById(R.id.line);
		
		findViewById(R.id.edit_content).setOnClickListener(this);
		if(mServiceTypeId==null||mData==null){
			mServiceTypeId = getIntent().getStringExtra("SERVICE_TYPE_ID");
			mData=getIntent().getStringExtra("CONTENT");	
		}
		entities = parseToEntity(mData);
		if(entities!=null)
			
		initView(entities);
	}
	
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("SERVICE_TYPE_ID", mServiceTypeId);
		outState.putString("CONTENT", mData);
	}
	
	
	private List<ServiceTypeEntity> parseToEntity(String json){
		JSONArray array;
		List<ServiceTypeEntity> entities;
		try {
			array=new JSONArray(json);
			entities = new ArrayList<ServiceTypeEntity>();
			for (int i = 0; i < array.length(); i++) {
				ServiceTypeEntity entity=new ServiceTypeEntity();
				JSONObject object=(JSONObject) array.get(i);
				if(object.has("SERVICE_ITEM_DESC")){
					mEditText.setText(object.optString("SERVICE_ITEM_DESC"));
				}else{
					entity.setCREATE_TIME(object.optString("CREATE_TIME"));
					entity.setSERVICE_HOUR(object.optString("SERVICE_HOUR"));
					entity.setSERVICE_TYPE_ID(object.optString("SERVICE_TYPE_ID"));
					entity.setSERVICE_TYPE_SUB(object.optString("SERVICE_TYPE_SUB"));
					entity.setSERVICE_TYPE_SUB_ID(object.optString("SERVICE_TYPE_SUB_ID"));
					entity.setUSED_FLAG(object.optString("ISOPEN"));
					entities.add(entity);
				}
			}
		} catch (JSONException e) {
			return null;
		}
		return entities;
	}
	
	private void initView(List<ServiceTypeEntity> entities) {
		mView.removeAllViews();
		LinearLayout linelayout = new LinearLayout(this);
		linelayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
		linelayout.setOrientation(LinearLayout.VERTICAL);
		int size=entities.size();
		for (int i = 0; i <size; i++) {
			final ServiceTypeEntity typeEntity = entities.get(i);
			View view = LayoutInflater.from(this).inflate(R.layout.doctor_base_add_view_item, null);
			TextView name = (TextView) view.findViewById(R.id.name);
			TextView status = (TextView) view.findViewById(R.id.status);
			name.setText(typeEntity.getSERVICE_TYPE_SUB());
			if(typeEntity.getUSED_FLAG().equals("1")){
				status.setText("已开通");
			}else{
				status.setText("未开通");
			}
			view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// 进入子类
					Intent intent = new Intent(DoctorMarkCommon.this,DoctorCommonTalkOption.class);
					intent.putExtra("SERVICE_TYPE_SUB_ID", typeEntity.getSERVICE_TYPE_SUB_ID());
					intent.putExtra("title", typeEntity.getSERVICE_TYPE_SUB());
					intent.putExtra("SERVICE_TYPE_ID", typeEntity.getSERVICE_TYPE_ID());
					startActivityForResult(intent,1000);
				}
			});
			if (i == 0) {
				view.setBackgroundResource(R.drawable.textview_background_up);
			} else if (i ==size-1) {
				view.setBackgroundResource(R.drawable.textview_background_down);
			} else {
				view.setBackgroundResource(R.drawable.textview_background_middle);
			}
			if(size==1){
				view.setBackgroundResource(R.drawable.text_background_single);
			}
			linelayout.addView(view);
		}
		mView.addView(linelayout);
		mView.invalidate();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			Intent intent2 = getIntent();
			intent2.putExtra("isUpdate",true);
			setResult(RESULT_OK, intent2);
			finish();
			break;
		case R.id.edit_content:
			Intent intent = new Intent(this, CommonExplainActivity.class);
			intent.putExtra("title", "医生提示");  
			intent.putExtra("mContentCount",200);  //字数限制  默认是200
			intent.putExtra("content",  mEditText.getText().toString());  //内容
			startActivityForResult(intent, 2000);
			break;
		

	
		}}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode != Activity.RESULT_OK)return;
		switch (requestCode) {
		case 2000:
			if(data!=null&&data.hasExtra("content")){
				String contentText=data.getStringExtra("content");
				mEditText.setText(contentText);
				submitFeedback(contentText);
			}
			break;
		case 1000:
			if(data!=null&&data.hasExtra("SERVICE_TYPE_SUB_ID")){
				String contentText=data.getStringExtra("SERVICE_TYPE_SUB_ID");
				for (int i = 0; i < entities.size(); i++) {
					final ServiceTypeEntity typeEntity = entities.get(i);
					if(typeEntity.getSERVICE_TYPE_SUB_ID().equals(contentText)){
						typeEntity.setUSED_FLAG(data.getIntExtra("flag", 0)+"");
					}
				}
				initView(entities);
			}
			break;
		}
	}
	
	/**
	 * 提交意见内容
	 */
	private void submitFeedback(String str){
		HttpRestClient.doHttpEditExplain(mServiceTypeId,str,new AsyncHttpResponseHandler(this){
			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				try {
					JSONObject object=new JSONObject(content);
					if(object.has("error_message")){
						ToastUtil.showBasicShortToast(getApplicationContext(), object.optString("error_message"));
					}
				} catch (Exception e) {}
			}
		});
	}

}
